<?php
// +----------------------------
// | Created by PhpStorm
// +----------------------------
// | Author: YnnSon
// +----------------------------
namespace app\api\model;

use app\api\service\Token;
use app\lib\enum\CommentEnum;
use app\lib\enum\PageEnum;
use app\lib\exception\CurdException;
use app\lib\exception\ParameterException;
use app\lib\exception\SuccessMessage;
use think\Db;
use think\Exception;

/**
 * 评论列表
 * Class Message
 * @package app\api\model
 */
class Comment extends BaseModel
{
    protected $autoWriteTimestamp = true; // 自动写入时间戳

    /**
     * @Desc 格式化日期
     *
     * @param $value
     *
     * @return false|string
     */
    protected function getCreateTimeAttr($value)
    {
        return date('Y.m.d', $value);
    }

    /**
     * @Desc 获取检车站名称
     *
     * @param $value
     * @param $data
     *
     * @return bool|float|mixed|string|null
     */
    protected function getCompanyNameAttr($value, $data)
    {
        return Db::name('company')->where('id', '=', static function ($query) use ($data) {
            $query->name('order')->where('order_id', '=', $data['order_id'])->field('company_id');
        })->value('company_name');
    }

    /**
     * @Desc 获取检车站名称
     *
     * @param $value
     * @param $data
     *
     * @return bool|float|mixed|string|null
     */
    protected function getNickNameAttr($value, $data)
    {
        return Db::name('user_weixin')->where('id', '=', $data['user_weixin_id'])->value('nick_name');
    }

    /**
     * @Desc 获取检车站名称
     *
     * @param $value
     * @param $data
     *
     * @return bool|float|mixed|string|null
     */
    protected function getAvatarUrlAttr($value, $data)
    {
        return Db::name('user_weixin')->where('id', '=', $data['user_weixin_id'])->value('avatar_url');
    }

    /**
     * @Desc 获取检车站名称
     *
     * @param $value
     * @param $data
     *
     * @return bool|float|mixed|string|null
     */
    protected function getVehicleNameAttr($value, $data)
    {
        return Db::name('order')->where('order_id', '=', $data['order_id'])->value('vehicle_name');
    }

    /**
     * @Desc 获取检车项目名称
     *
     * @param $value
     * @param $data
     *
     * @return bool|float|mixed|string|null
     */
    protected function getGoodsNameAttr($value, $data)
    {
        return Db::name('order')->where('order_id', '=', $data['order_id'])->value('goods_name');
    }

    /**
     * @Desc 获取订单金额
     *
     * @param $value
     * @param $data
     *
     * @return int
     */
    protected function getTotalAmountAttr($value, $data)
    {
        $total_amount = Db::name('order')->where('order_id', '=', $data['order_id'])->value('total_amount');
        return (int)$total_amount;
    }

    /**
     * @Desc 获取订单金额
     *
     * @param $value
     * @param $data
     *
     * @return bool|float|mixed|string|null
     */
    protected function getImagesAttr($value, $data)
    {
        $images = [];
        $urls = Db::name('image')->whereIn('id', $data['image_id'])->column('url');
        foreach ($urls as $url) {
            $images[] = $this->addApiUrlPrefixToImage($url);
        }
        return $images;
    }

    /**
     * @Desc 获取评论列表
     *
     * @param $page
     *
     * @return mixed
     */
    public static function getCommentList($page)
    {
        $user_weixin_id = Token::getUidFromCache();
        $listRows = PageEnum::COMMENTLIST; // 分页数量
        $status = CommentEnum::YSHOW; // 显示状态
        $field = 'id, info, image_id, order_id, create_time';
        $comment = (new self)->field($field)->where('user_weixin_id', '=', $user_weixin_id)->where('status', '=', $status)->order('create_time desc')->page($page, $listRows)->select();
        return $comment->append(['company_name', 'goods_name', 'total_amount', 'images'])->hidden(['order_id', 'image_id'])->toArray();
    }

    /**
     * @Desc 获取评论列表
     *
     * @param $id
     * @param $page
     *
     * @return mixed
     */
    public static function getListFromStation($id, $page)
    {
        $listRows = PageEnum::COMMENTLIST; // 分页数量
        $status = CommentEnum::YSHOW; // 显示状态
        $field = 'id, info, image_id, order_id, user_weixin_id, create_time';
        $comment = (new self)->field($field)->where('status', '=', $status)->where('company_id', '=', $id)->order('create_time desc')->page($page, $listRows)->select();
        return $comment->append(['nick_name', 'avatar_url', 'goods_name', 'vehicle_name', 'images'])->hidden(['order_id', 'image_id', 'user_weixin_id'])->toArray();
    }

    /**
     * @Desc 添加评论
     *
     * @param $param
     *
     * @return SuccessMessage
     */
    public static function insertComment($param)
    {
        Db::startTrans();
        try {
            $user_weixin_id = Token::getUidFromCache();
            $comment = (new self)->where('order_id', '=', $param['order_id'])->where('user_weixin_id', '=', $user_weixin_id)->find();
            if($comment){
                throw new ParameterException([
                    'message' => '您已评价过，请刷新'
                ]);
            }
            $image_id = Image::insertImage($param['images']);
            $insertData = [
                'user_weixin_id' => $user_weixin_id,
                'company_id'       => $param['company_id'],
                'order_id'       => $param['order_id'],
                'info'           => $param['info'],
                'image_id'       => $image_id,
            ];
            if (!self::create($insertData)) {
                throw new CurdException();
            }
            Db::commit();
            return new SuccessMessage();
        } catch (Exception $e) {
            Db::rollback();
            throw $e;
        }
    }
}